<template>
	<div class="element">
	  <el-form :inline="true" :model="elementSearch" label-width="70px" style="min-width: 1200px;" class='searchForm'>
		<el-form-item label="元素名称">
			<el-input v-model="elementSearch.name" placeholder="请输入" clearable/>
		</el-form-item>
		<el-form-item label="定位方式">
			<el-select v-model="elementSearch.by" style="width: 100%;">
				<el-option v-for="value in by_list" :label="value" :value="value" ></el-option>
			</el-select>
		</el-form-item>
		<el-form-item label="所属平台" style="margin-right: 0px;">
		</el-form-item>
		<el-cascader collapse-tags v-model='elementSearch.page_list' :options="module_page_list" :props="props" clearable style="margin-bottom: 15px; margin-right: 32px;"/>
		<el-form-item>
			<el-button type="primary" @click="reset">重置</el-button>
			<el-button type="primary" @click="search">搜索</el-button>
		</el-form-item>
	  </el-form>
	  <el-dialog v-model="editDialogVisible" :title="title" style="width: 400px;" class='dialog'>
		  <el-form :model="elementSave" label-position='right' label-width="70px">
			<el-form-item label="元素名称">
				<el-input v-model="elementSave.name" autocomplete="off" placeholder="请输入"/>
			</el-form-item>
			<el-form-item label="定位方式">
				<el-select v-model="elementSave.by" style="width: 100%;">
					<el-option v-for="value in by_list" :label="value" :value="value" ></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="元素值">
				<el-input v-model="elementSave.value" autocomplete="off" placeholder="请输入"/>
			</el-form-item>
			<el-form-item label="所属页面">
				<el-cascader collapse-tags v-model='elementSave.page' :options="module_page_list" :props="editProps" clearable style="margin-bottom: 15px; margin-right: 32px;"/>
			</el-form-item>
		  </el-form>
		  <template #footer>
			<span class="dialog-footer">
			  <el-button @click="editDialogVisible = false">取消</el-button>
			  <el-button type="primary" @click="save">
				确认
			  </el-button>
			</span>
		  </template>
	  </el-dialog>
	  <el-table :data="page_list.results" class='table'>
		<el-table-column label="序号" width="70" type="index"/>
		<el-table-column label="元素名称" prop="name" min-width="100"/>
		<el-table-column label="定位方式" prop="by" min-width="100"/>
		<el-table-column label="元素值" prop="value" min-width="100"/>
		<el-table-column label="页面名称" prop="page_name" min-width="100"/>
		<el-table-column label="模块名称" prop="module_name" min-width="100"/>
		<el-table-column label="平台名称" prop="plant_name" min-width="100"/>
		<el-table-column label="创建人" prop="create_by_name"  min-width="150"/>
		<el-table-column label="更新人" prop="update_by_name"  min-width="150"/>
		<el-table-column label="创建时间" prop="create_time" sortable min-width="150"/>
		<el-table-column label="更新时间" prop="update_time" sortable min-width="150"/>
		<el-table-column align="center" min-width="180">
			<template #header>
				<el-button v-if='permission.has_add_permission' type='primary' @click="addElement">新增</el-button>
			</template>
			<template #default="scope">
			  <el-button v-if="permission.has_edit_permission" :icon="Edit" size="small" type='primary' @click="editElement(scope.row)">编辑</el-button
			  >
			  <el-button v-if="permission.has_delete_permission"
				@click="deleteElement(scope.row.id)"
				size="small"
				type="danger"
			  :icon="Delete"
				>删除</el-button
			  >
			</template>
		</el-table-column>
	  </el-table>
	  <div style="margin: 10px">
	  	<el-pagination
	  	  v-model:current-page="page_size_params.page"
	  	  v-model:page-size="page_size_params.size"
	  	  :page-sizes="[10, 20, 30, 50]"
	  	  :small="small"
	  	  :disabled="disabled"
	  	  :background="background"
	  	  :hide-on-single-page="false"
	  	  layout="total, sizes, prev, pager, next, jumper"
	  	  :total="page_list.count"
	  	  @size-change="handleSizeChange"
	  	  @current-change="handleCurrentChange"
	  	/>
	  </div>
	</div>
</template>

<script>
import {mapState, mapActions, mapGetters} from 'vuex'
import { ElMessage, ElMessageBox } from 'element-plus'
import { View, Delete, Edit, Plus, EditPen} from '@element-plus/icons-vue'

export default{
	computed:{
		...mapState(['pathPermission', 'projectInfo']),
	},
	data() {
		return {
			props: {
				multiple: true,
				emitPath: false,
				value: 'id',
				label: 'name',
			},
			by_list:['id', 'xpath', 'link text', 'partial link text', 'name', 'tag name', 'class name', 'css selector'],
			editProps:{
				emitPath: false,
				value: 'id',
				label: 'name',
			},
			permission: {},
			elementSearch:{
				name: '',
				project: '',
				by: '',
				page_id: '',
				page_list: [],
			},
			page_size_params: {
				page: 1,
				size: 10,
			},
			count: 1,
			title: '新增页面',
			isAdd: true,
			editDialogVisible: false,
			page_list: [],
			module_page_list: [],
			ViewVisible: false,
			elementSave:{
				project: '',
				name: '',
				page: '',
				by: '',
				value: '',
			},
			role_names: [],
		}
	},
	setup() {
		return {
			Edit,
			Delete,
			Plus,
			View,
			EditPen,
		}
	},
	methods:{
		...mapActions(['getRolePermission']),
		handleCurrentChange(){
			this.getElements()
		},
		handleSizeChange(){
			this.getElements()
		},
		search(){
			this.getElements()
		},
		reset(){
			for(let key in this.elementSearch){
				if(key === 'page_list'){
					this.elementSearch[key] = []
				}else{
					this.elementSearch[key] = ''
				}
				
			}
		},
		editElement(row_data){
			this.isAdd = false
			this.title = '编辑元素'
			this.getElement(row_data.id)
			this.editDialogVisible = true
		},
		addElement(){
			this.elementSave = {}
			this.title = '新增元素'
			this.editDialogVisible = true
			this.isAdd = true
		},
		save(){
			if (this.isAdd){
				this.createElement()
			}else{
				this.updateElement()
			}
		},
		async createElement(){
			this.elementSave.project = this.projectInfo.id
			const response = await this.$api.createElement(this.elementSave)
			if(response.status === 201){
				this.editDialogVisible = false
				this.getElements()
			}
		},
		async updateElement(){
			this.elementSave.project = this.projectInfo.id
			const response = await this.$api.updateElement(this.elementSave.id, this.elementSave)
			if(response.status === 200){
				this.editDialogVisible = false
				this.getElements()
			}
		},
		async deleteElement(id){
			const response = await this.$api.deleteElement(id)
			if (response.status === 204){
				this.getElements()
			}
		},
		async getElement(id){
			const response = await this.$api.getElement(id)
			if (response.status === 200){
				this.elementSave = {...response.data.result}
			}
		},
		async getModulePage(){
			const response = await this.$api.getAllModulePage({project: this.projectInfo.id})
			if (response.status === 200){
				this.module_page_list = response.data.results
			}
		},
		async getElements(){
			this.elementSearch.project = this.projectInfo.id
			this.elementSave.project = this.projectInfo.id
			this.elementSearch.page_id = this.elementSearch.page_list.join(',')
			const response = await this.$api.getElements(Object.assign(this.elementSearch, this.page_size_params))
			if (response.status === 200){
				this.page_list = {...response.data}
			}
		},
	},
	created() {
		this.getRolePermission(this.pathPermission[this.$route.path]).then(res =>{
			this.permission = {...res.result}
		})
		this.getModulePage()
		this.getElements()
		
	}
}
</script>

<style scoped>
	.element{
		width: 100%;
		display: flex;
		flex-direction: column;
		overflow: hidden;
	}
	.element .searchForm{
		width: 100%;
		flex: 1;
		background-color: white;
		padding: 15px 15px 0 15px;
		margin-bottom: 5px;
	}
	.element .el-form--inline .el-form-item{
		margin-bottom: 15px;
	}
	.element .table{
		background-color: white;
		flex: 1;
	}
	.searchForm .el-input{
		width: 150px;
	}
	.searchForm .el-select{
		width: 150px;
	}
	.dialog .el-input{
		width: 300px;
	}
	.dialog .el-select{
		width: 300px;
	}
	/deep/ .dialog .el-input__wrapper{
		width: 270px;
	}
	.form .el-select{
		width: 300px;
	}
	.el-button{
		width: 70px;
	}
	.el-icon{
		margin-left: 10px;
	}
	
</style>
